package net.qiqbframework.persisthanding.dao;

import org.javers.core.Javers;
import org.javers.core.JaversBuilder;
import org.javers.core.diff.ListCompareAlgorithm;
import org.javers.core.metamodel.clazz.EntityDefinition;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BasicEntityPoDiff {

    private static final Map<Class<?>, Javers> javersCache = new ConcurrentHashMap<>();

    public static <Po extends BasicEntityPo> Javers getJavers(Class<Po> entityClass) {
        Javers javers = javersCache.get(entityClass);
        if (javers == null) {
            synchronized (BasicEntityPoDiff.class) {
                javers = JaversBuilder.javers()
                        .registerEntity(new EntityDefinition(entityClass, "id"))
                        .withListCompareAlgorithm(ListCompareAlgorithm.AS_SET).build();
                javersCache.put(entityClass, javers);
            }
        }
        return javers;
    }
}
